﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace UCTT.NetFx
{
    public partial class Form2 : Form
    {
        /*泛型集合用于图表显示*/
        private List<string> List_ia = new List<string>();    //电流ia
        private List<int> List_ic = new List<int>();    //电流ic
        private List<int> List_udc = new List<int>();    //母线电压dc
        private List<int> List_vel = new List<int>();    //速度
        private int numstart = 95;
        private bool isToTurn = false;// 在95~805之间累积时是否转向

        Random random = new Random();

        public Form2()
        {
            InitializeComponent();
        }

        //界面初始化
        private void Form1_Load(object sender, EventArgs e)   
        {

        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            int num1 = random.Next(95, 805);   //随机数
            int num2 = random.Next(0, 4);
            //int num3 = random.Next(0, 99);
            //int num4 = random.Next(0, 99);

            if (isToTurn)
            {
                numstart -= 5;
                if (numstart <= 95)
                {
                    isToTurn = false;
                    //chart1.ChartAreas[0].AxisX.IsReversed = false;
                }
            }
            else
            {
                numstart += 5;
                if (numstart >= 405)
                {
                    isToTurn = true;
                    //chart1.ChartAreas[0].AxisX.IsReversed = true;
                }
            }

            List_ia.Add(numstart.ToString()); ListRemove(List_ia);
            List_ic.Add(num2); ListRemove(List_ic);
            //List_udc.Add(num3); ListRemove(List_udc);
            //List_vel.Add(num4); ListRemove(List_vel);

            DrawChart(List_ia, List_ic, List_udc, List_vel);
        }


        private void DrawChart(List<string> List_ia, List<int> List_ic, List<int> List_udc, List<int> List_vel)
        {
            chart1.Series[0].Points.Clear();    //清除所有点
            //chart1.Series[1].Points.Clear();
            //chart2.Series[0].Points.Clear();
            //chart3.Series[0].Points.Clear();

            for (int i = 0; i < List_ia.Count; i++)
            {
                chart1.Series[0].Points.AddXY(List_ia[i], List_ic[i]);   //添加点
            }
            //for (int i = 0; i < List_ic.Count; i++)
            //{
            //    chart1.Series[1].Points.AddXY(i + 1, List_ic[i]);
            //}
            //for (int i = 0; i < List_udc.Count; i++)
            //{
            //    chart2.Series[0].Points.AddXY(i + 1, List_udc[i]);
            //}
            //for (int i = 0; i < List_vel.Count; i++)
            //{
            //    chart3.Series[0].Points.AddXY(i + 1, List_vel[i]);
            //}
        }

        //清除所有的列表
        private void ClearAllList()
        {
            List_ia.Clear();
            List_ic.Clear();
            List_udc.Clear();
            List_vel.Clear();
        }

        //当大于80时移除list头部
        private void ListRemove<T>(List<T> list)
        {
            if (list.Count >= 20)
            {
                list.RemoveAt(0);
            }
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            if (btn_Start.Text == "开始")
            {
                timer1.Start();
                btn_Start.Text = "暂停";
            }
            else
            {
                timer1.Stop();
                btn_Start.Text = "开始";
            }
        }
    }
}
